<?php

namespace StudyBuddy\Api;

use StudyBuddy\MongoDoc;

/**
 * Class represents a registered API client
 * API client has unique client ID and is allowed to access
 * OUR API
 */
class Clientdata extends MongoDoc {

    protected $aReturn = array('app', 'api_key');

    public function __construct(\StudyBuddy\Registry $oRegistry) {

        parent::__construct($oRegistry, 'API_CLIENTS');
    }

    /**
     * Factory method
     * @todo when support for OAuth2 is added
     * then we will check if OAuth2 is enable - return
     * OAuth2 sub-class.
     * It will have extra methods
     * to return and save extra data
     *
     * @param Registry $oRegistry
     */
    public static function factory(\StudyBuddy\Registry $oRegistry) {
        return new static($oRegistry);
    }

    /**
     * Get SRC if the icon image
     * Will return default path
     * to a generic app icon
     * if this client has not uploaded
     * own icon.
     *
     * @return string value of the $src of the image
     */
    public function getIcon($asHtml = true) {
        $s = $this->offsetGet('icon');

        $s = (empty($s)) ? '/images/app2.png' : STUDYBUDDY_AVATAR_IMG_SITE . \StudyBuddy\PATH_WWW_IMG_AVATAR_SQUARE . $s;

        if (!$asHtml) {
            return $s;
        }

        return '<img src="' . $s . '" width="72px" height="72px" alt="App Logo">';
    }

    /**
     * Return array of key->value
     * only return certain keys that should
     * be displayed on the Cliend info page
     * This includes api_key, name
     * In case of OAuth2 sub-class show
     * app token as well as urls for end-points
     * related to OAuth2 authorization and
     * other important urls
     *
     * @return array
     */
    public function getData() {
        $ret = array(
            'Application' => $this->offsetGet('app_name'),
            'About' => '<span class="pre">' . $this->offsetGet('about') . '</span>',
            'Icon' => $this->getIcon(true),
            'API Key' => $this->offsetGet('api_key'));

        return $ret;
    }

}
